package com.zone.gateway.sdk.postProcess;

import com.zone.gateway.sdk.annotation.ApiProductClazz;
import com.zone.gateway.sdk.annotation.ApiProductMethod;
import com.zone.gateway.sdk.config.ApiGatewaySdkProperties;
import com.zone.gateway.sdk.domain.model.entity.ApplicationInterfaceEntity;
import com.zone.gateway.sdk.domain.model.entity.ApplicationInterfaceMethodEntity;
import com.zone.gateway.sdk.domain.model.entity.ApplicationSystemEntity;
import com.zone.gateway.sdk.domain.service.ApplicationConfigRegisManageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: zongzi
 * @description: api 生产者bean后置处理器
 * @date: 2025/4/29
 */
public class ApiProductBeanPostProcess implements BeanPostProcessor {
    private final Logger logger = LoggerFactory.getLogger(ApiProductBeanPostProcess.class);

    private final ApplicationConfigRegisManageService applicationConfigRegisManageService;
    private final ApiGatewaySdkProperties properties;

    public ApiProductBeanPostProcess(ApplicationConfigRegisManageService applicationConfigRegisManageService, ApiGatewaySdkProperties properties) {
        this.applicationConfigRegisManageService = applicationConfigRegisManageService;
        this.properties = properties;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        Class<?> beanClazz = bean.getClass();
        // 判断是否标注生产者注解
        ApiProductClazz productClazzAnnotation = beanClazz.getAnnotation(ApiProductClazz.class);
        if (null == productClazzAnnotation) {
            return bean;
        }
        this.validProductClass(productClazzAnnotation);
        // 处理方法
        List<ApplicationInterfaceMethodEntity> methodEntityList = new ArrayList<>();
        for (Method method : beanClazz.getMethods()) {
            ApiProductMethod productMethod = method.getAnnotation(ApiProductMethod.class);
            if (null == productMethod) {
                continue;
            }
            // 参数类型全限定名：限定一个
            Parameter[] parameters = method.getParameters();
            if (parameters.length > 1) {
                throw new RuntimeException("参数列表不能超过一个");
            }
            String parameterType = parameters[0].getType().getName();
            // 校验方法中注解信息是否完善
            this.validProductMethod(productMethod);

            ApplicationInterfaceMethodEntity interfaceMethod = new ApplicationInterfaceMethodEntity();
            interfaceMethod.setSystemId(properties.getSystemId());
            interfaceMethod.setInterfaceId(productClazzAnnotation.interfaceClass().getName());
            interfaceMethod.setMethodId(method.getName());
            interfaceMethod.setMethodName(productMethod.methodDesc());
            interfaceMethod.setParameterType(parameterType);
            interfaceMethod.setUri(productMethod.uri());
            interfaceMethod.setHttpCommandType(productMethod.httpCommandType());
            interfaceMethod.setAuth(productMethod.auth());
            methodEntityList.add(interfaceMethod);
        }

        // 注册应用
        ApplicationSystemEntity applicationSystemEntity = new ApplicationSystemEntity();
        applicationSystemEntity.setSystemId(properties.getSystemId());
        applicationSystemEntity.setSystemName(properties.getSystemName());
        applicationSystemEntity.setSystemType(properties.getSystemType());
        applicationSystemEntity.setSystemRegistry(properties.getSystemRegistry());
        applicationConfigRegisManageService.regisApplicationSystem(applicationSystemEntity);
        // 注册接口
        ApplicationInterfaceEntity applicationInterfaceEntity = new ApplicationInterfaceEntity();
        applicationInterfaceEntity.setSystemId(properties.getSystemId());
        applicationInterfaceEntity.setInterfaceId(productClazzAnnotation.interfaceClass().getName());
        applicationInterfaceEntity.setInterfaceName(productClazzAnnotation.interfaceName());
        applicationInterfaceEntity.setInterfaceVersion(productClazzAnnotation.interfaceVersion());
        applicationConfigRegisManageService.regisApplicationInterface(applicationInterfaceEntity);
        // 注册方法
        methodEntityList.forEach(applicationConfigRegisManageService::regisApplicationMethod);
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }

    private void validProductClass(ApiProductClazz productClazzAnnotation) {
        if (null == productClazzAnnotation.interfaceClass()) {
            throw new RuntimeException("ApiProductClazz annotation interfaceClass must be value");
        }
        if (null == productClazzAnnotation.interfaceVersion()) {
            throw new RuntimeException("ApiProductClazz annotation interfaceVersion must be value");
        }
    }

    private void validProductMethod(ApiProductMethod productMethod) {
        if (null == productMethod.uri()) {
            throw new RuntimeException("ApiProductMethod annotation uri must be value");
        }
        if (null == productMethod.httpCommandType()) {
            throw new RuntimeException("ApiProductMethod annotation httpCommandType must be value");
        }
    }
}
